package JavaSEAdvance.day02_interface.d022_Polymorpic;

/**
 * @author Xiao
 * @date 2021/7/16
 */
public class Test {
    public static void main(String[] args) {
        Animal a = new Cat();   //向上转型：父类引用指向子类对象
        Cat c = (Cat) a;    //向下转型：子类引用指向父类对象，强制转换
        useAnimal(a);
        System.out.println("---------");
        useAnimal(new Dog());
        System.out.println("---------");
        useAnimal(new Pig());
    }

    public static void useAnimal (Animal a) {
        a.eat();
        if (a instanceof Cat) {     //判断对象a是否属于Cat类
            ((Cat) a).catching();
        } else if (a instanceof Dog) {
            ((Dog) a).watching();
        } else {
            return;
        }
    }
}
